﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QL_Nha_Sach_Phuong_Nam.Quản_lý_sách.Quản_lý_tác_giả
{
    public partial class FormEditTacGia : Form
    {
        private Cxulytacgia xuly;
        string maedittg;
        int id;
        string no;

        public FormEditTacGia(int button,string ma)
        {
            InitializeComponent();
            if (button == 0)
                btnchung.Text = "Add";
            else
                btnchung.Text = "Save";
            maedittg = ma;
        }

        private void FormEditTacGia_Load(object sender, EventArgs e)
        {
            xuly = new Cxulytacgia();
            if (btnchung.Text == "Save")
            {
                Kết_nối.TacGia editncc = xuly.TimTacgiatheoma(maedittg);
                if (editncc != null && editncc.TacGiaStatus == "live")
                {
                    id = editncc.TacGiaID;
                    no = editncc.TacGiaNo;
                    txtten.Text = editncc.TacGiaName;
                    txtdiachi.Text = editncc.TacGiaAddress;
                    txtsdt.Text = editncc.TacGiaPhoneNumber;
                    if (editncc.TacGiaSex == "Nam")
                        rdbNam.Checked = true;
                    else
                        rdbNu.Checked = true;
                    txtdes.Text = editncc.TacGiaDescription;
                }
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnchung_Click(object sender, EventArgs e)
        {
            if (btnchung.Text == "Add")
            {
                Kết_nối.TacGia ncc = new Kết_nối.TacGia();
                ncc.TacGiaID = xuly.getdsTacgia().Last().TacGiaID + 1;
                ncc.TacGiaNo = string.Concat("TG", ncc.TacGiaID.ToString());
                ncc.TacGiaName = txtten.Text;
                ncc.TacGiaAddress = txtdiachi.Text;
                ncc.TacGiaPhoneNumber = txtsdt.Text;
                if (rdbNam.Checked == true)
                    ncc.TacGiaSex = "Nam";
                else
                    ncc.TacGiaSex = "Nữ";
                ncc.TacGiaDescription = txtdes.Text;
                ncc.TacGiaStatus = "live";
                xuly.ThemTacgia(ncc);
                Close();
            }
            else
            {
                Kết_nối.TacGia ncc = new Kết_nối.TacGia();
                ncc.TacGiaID = id;
                ncc.TacGiaNo = no;
                ncc.TacGiaName = txtten.Text;
                ncc.TacGiaAddress = txtdiachi.Text;
                ncc.TacGiaPhoneNumber = txtsdt.Text;
                if (rdbNam.Checked == true)
                    ncc.TacGiaSex = "Nam";
                else
                    ncc.TacGiaSex = "Nữ";
                ncc.TacGiaDescription = txtdes.Text;
                xuly.SuaTacgia(ncc);
                Close();
            }
        }
    }
}
